package com.huaxiafinance.management.controller;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaxiafinance.management.model.Permission;
import com.huaxiafinance.management.service.PermissionService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller("permissionController")
@RequestMapping("/permission")
public class PermissionController {

  private static final Logger log = Logger.getLogger(RoleController.class);

  @Autowired
  private PermissionService permissionService;

  @RequestMapping(value = "/addPermission", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String addPermission(HttpServletRequest request) throws Exception {
    String json = request.getParameter("json");

    log.info("新增菜单请求参数" + json);

    MessageResult messageResult = new MessageResult();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Permission Permission = mapper.readValue(json, Permission.class);

    boolean flag = permissionService.addPermission(Permission);

    if (flag) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    log.info("新增菜单返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/queryPermission", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryPermission(HttpServletRequest request) throws Exception {
    MessageResult messageResult = new MessageResult();

    List<Permission> list = permissionService.queryPermission();
    messageResult.setData(list);
    messageResult.setResultMessage(ServiceCode.SUCCESS);

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/updatePermission", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updatePermission(HttpServletRequest request) throws Exception {
    String json = request.getParameter("json");

    log.info("修改菜单请求参数" + json);

    MessageResult messageResult = new MessageResult();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Permission permission = mapper.readValue(json, Permission.class);

    boolean flag = permissionService.updatePermission(permission);

    if (flag) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    log.info("修改菜单返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/delPermission", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String delPermission(Integer id) throws Exception {

    log.info("删除角色请求参数" + id);

    MessageResult messageResult = new MessageResult();

    boolean flag = permissionService.deletePermission(id);

    if (flag) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    log.info("删除角色返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }
}
